import 'package:dog_read/db/book_db.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite/sqflite.dart';

class PageControlProvider with ChangeNotifier {
  bool _isAllowScroll = true;
  bool _pageSnapping = true;
  Axis _pageTurningDirection = Axis.horizontal;
  int _currentChapterPage = 1;
  double _fontSize = 20.0;
  double _lineHeight = 2.0;

  Future setSettings() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool hasSettings = prefs.getBool("hasSettings") ?? false;
    if (!hasSettings) {
      return;
    }
    this._pageSnapping = prefs.getBool("pageSnap");
    this._fontSize = prefs.getDouble("pageFontSize");
    this._lineHeight = prefs.getDouble("pageLineHeight");
    this._pageTurningDirection = prefs.getBool("pageDirection")
        ? Axis.vertical
        : Axis.horizontal; // true上下滑动
  }

  void setFontSize(double fontSize) {
    this._fontSize = fontSize;
    notifyListeners();
  }

  double get fontSize => this._fontSize;

  void setLineHeight(double height) {
    this._lineHeight = height;
    notifyListeners();
  }

  double get lineHeight => this._lineHeight;

  void setAllowScroll(bool allow) {
    this._isAllowScroll = allow;
    notifyListeners();
  }

  void setPageSnapping(bool pageSnapping) {
    this._pageSnapping = pageSnapping;
    notifyListeners();
  }

  void setPageTurningDirection(Axis direction) {
    this._pageTurningDirection = direction;
    notifyListeners();
  }

  void setCurrentChapterPage(int page) {
    this._currentChapterPage = page;
    notifyListeners();
  }

  get currentChapterPage => this._currentChapterPage;
  get pageSnapping => this._pageSnapping;
  get pageTurningDirection => this._pageTurningDirection;
  get isAllowScroll => this._isAllowScroll;
}

class ChapterInfosProvider with ChangeNotifier {
  List _chapterList;
  int _chapterIndex;
  String _bookTitle;
  int _bookId;

  void setBookId(int id){
    this._bookId=id;
  }
  int get bookId => this._bookId;

  void setChapterList(List chapterList) {
    this._chapterList = chapterList;
  }

  List get chapterList => this._chapterList;

  void setChapterIndex(int index) async{
    this._chapterIndex = index;
    Database db = await bookDb;
    await db.rawUpdate("update book set recent_read = ? where id=?", [this._chapterIndex, this._bookId]); 
    notifyListeners();
  }

  get chapterIndex => this._chapterIndex;

  void setBookTitle(String title) {
    this._bookTitle = title;
  }

  get bookTitile => this._bookTitle;
}

class BookshelfProvider with ChangeNotifier {
  List _selectedBooks = [];

  void addBook(String bookId) {
    this._selectedBooks.add(bookId);
    notifyListeners();
  }

  void removeBook(String bookId) {
    this._selectedBooks.remove(bookId);
    notifyListeners();
  }

  void clearBooks() {
    this._selectedBooks = [];
    notifyListeners();
  }

  void replaceAll(List list) {
    this._selectedBooks = list;
    notifyListeners();
  }

  List get books => this._selectedBooks;
}

class ShelfProvider with ChangeNotifier {

  List _shelfBooks = [];


  void initShelfBooks() async {
    Database db = await bookDb;
    this._shelfBooks =
        await db.rawQuery("select * from bookshelf order by create_time desc");
    notifyListeners();
  }

  void setShelfBooks(List list) {
    this._shelfBooks = list;
    notifyListeners();
  }

  List get shelfBooks => this._shelfBooks;
}
